package info.ask4research.web.dto;

import info.ask4research.model.activities.SupportActivity;
import info.ask4research.model.roles.Role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class ActivityRolesDTO {

	String roleName;
	int id;
	boolean supportedRole;
	
	HashMap<Integer, ActivityRolesDTO> roles = new HashMap<Integer, ActivityRolesDTO>();
	List<ActivityRolesDTO> rolesDTO = new ArrayList<ActivityRolesDTO>();
	
	
	public ActivityRolesDTO(){}
	
	public ActivityRolesDTO(SupportActivity supportActivity,List<Role> unitOfLearningroles){
			supportActivityServiceConstructor(supportActivity, unitOfLearningroles);
	}

	private void supportActivityServiceConstructor(SupportActivity supportActivity,
			List<Role> unitOfLearningroles) {
		List<Role> supportedRoles =  supportActivity.getSupportedRoles();
		ActivityRolesDTO activityRolesDTO;
		if(supportedRoles.isEmpty() ){
			for(Role role : unitOfLearningroles){
				activityRolesDTO = new ActivityRolesDTO();
				activityRolesDTO.setId(role.getId());
				activityRolesDTO.setRoleName(role.getText());
				activityRolesDTO.setSupportedRole(false);
				roles.put(role.getId(), activityRolesDTO);
			}
			
		}

		if(!supportedRoles.isEmpty()){
			for(Role role : supportedRoles){
				activityRolesDTO = new ActivityRolesDTO();
				activityRolesDTO.setRoleName(role.getText());
				activityRolesDTO.setId(role.getId());
				activityRolesDTO.setSupportedRole(true);
				roles.put(role.getId(), activityRolesDTO);
			}
			for(int i =0; i<unitOfLearningroles.size(); i++){
				if(!roles.containsKey(unitOfLearningroles.get(i).getId())){
					activityRolesDTO = new ActivityRolesDTO();
					activityRolesDTO.setId(unitOfLearningroles.get(i).getId());
					activityRolesDTO.setRoleName(unitOfLearningroles.get(i).getText());
					activityRolesDTO.setSupportedRole(false);
					roles.put(unitOfLearningroles.get(i).getId(), activityRolesDTO);
				}
			}
		}
		Iterator<ActivityRolesDTO> it = roles.values().iterator();
		while(it.hasNext()){
			rolesDTO.add((ActivityRolesDTO) it.next());
		}
	}

	
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public HashMap<Integer, ActivityRolesDTO> getRoles() {
		return roles;
	}

	public void setRoles(HashMap<Integer, ActivityRolesDTO> roles) {
		this.roles = roles;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<ActivityRolesDTO> getRolesDTO() {
		return rolesDTO;
	}

	public void setRolesDTO(List<ActivityRolesDTO> rolesDTO) {
		this.rolesDTO = rolesDTO;
	}

	public boolean isSupportedRole() {
		return supportedRole;
	}

	public void setSupportedRole(boolean supportedRole) {
		this.supportedRole = supportedRole;
	}


}
